package com.first.nell.info.cotediva

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.first.nell.base.BaseActivity
import com.first.nell.data.ReqSaveWalletFcFa
import com.first.nell.databinding.ActivityFirstNellBindWalletOrangeBinding
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.*
import com.first.nell.launch.UserStateViewModel

@SuppressLint("CustomSplashScreen")
class BindWalletTypeOrangeActivity : BaseActivity() {

    private lateinit var mFirstNellActivityFirstNellBinding: ActivityFirstNellBindWalletOrangeBinding

    private val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    private val mWalletInfoViewModel by lazy {
        ViewModelProvider(this)[WalletInfoViewModel::class.java]
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        list.add(mWalletInfoViewModel)
        return list
    }

    override fun onFirstNellBindingFirstNellViewFirstNellTag(): String {
        return PageTagEnum.wallet_wave_fcfa.name.uppercase()
    }

    override fun onFirstNellBindingFirstNellContentFirstNellView(): View {
        mFirstNellActivityFirstNellBinding = ActivityFirstNellBindWalletOrangeBinding.inflate(layoutInflater)
        return mFirstNellActivityFirstNellBinding.root
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onFirstNellContentFirstNellViewFirstNellBinded() {
        try {
            //获取用户状态回调
            mUserStateViewModel.onFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.launch)
                }
            }

            //保存用户钱包回调
            mWalletInfoViewModel.onFirstNellSaveFirstNellWalletFirstNellFCFAFirstNellLiveData.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { _ ->
                        //跳转展示列表
                        startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<WalletListAddResultActivity>()
                    }
                }
            }

            //点击保存
            mFirstNellActivityFirstNellBinding.submitBindWalletOrange.setOnClickListener {
                val formDataCompleteState =
                    isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted()
                mFirstNellActivityFirstNellBinding.submitBindWalletOrange.recordFirstNellClickFirstNellEvent(
                    formDataCompleteState
                )
                if (formDataCompleteState.isBlank()) {
                    //保存
                    mWalletInfoViewModel.saveFirstNellWalletFirstNellFCFA(
                        ReqSaveWalletFcFa(
                            walletMobile = mFirstNellActivityFirstNellBinding.nivWalletNumber.getFirstNellContent(),
                            walletMobileConfirm = mFirstNellActivityFirstNellBinding.nivWalletNumberConfirm.getFirstNellContent(),
                            walletChannel = "ORANGE"
                        )
                    )
                }
            }

            //点击其它
            mFirstNellActivityFirstNellBinding.submitBindWalletOrangeOther.setOnClickListener {
                mFirstNellActivityFirstNellBinding.submitBindWalletOrangeOther.recordFirstNellClickFirstNellEvent()
                startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<BindWalletTypeMtnActivity>()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun isFirstNellAllFirstNellFormFirstNellDataFirstNellCompleted(): String {
        val notFillOptions = mutableListOf<String>()
        if (!mFirstNellActivityFirstNellBinding.nivWalletNumber.isFirstNellCompleted()) {
            val gTag = mFirstNellActivityFirstNellBinding.nivWalletNumber.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mFirstNellActivityFirstNellBinding.nivWalletNumberConfirm.isFirstNellCompleted()) {
            val gTag = mFirstNellActivityFirstNellBinding.nivWalletNumberConfirm.getFirstNellSafeFirstNellTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }
}